﻿using CommonServiceLocator;
using GalaSoft.MvvmLight.Ioc;
using Jn698._45GWTool.Models.DataGrid;
using Jn698._45GWTool.ViewModels;
using Jn698._45GWTool.ViewModels.Controls;
using Jn698._45GWTool.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Jn698._45GWTool.Controls
{
    /// <summary>
    /// AddOrUpdateTransPlanControl.xaml 的交互逻辑
    /// </summary>
    public partial class AddOrUpdateTransPlanControl : UserControl
    {
        public AddOrUpdateTransPlanControl()
        {
            InitializeComponent();
        }

        private void DataGrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            var list = ServiceLocator.Current.GetInstance<AddOrUpdateTransPlanViewModel>().ContentList;
            // 获取所点击的行索引和列索引
            int rowIndex = grd.SelectedIndex;//((selectedCell.Item as DataRowView).Row as DataRow).Table.Rows.IndexOf((selectedCell.Item as DataRowView).Row);//selectedCell.Item as DataRowView == null ? -1 : ((selectedCell.Item as DataRowView).Row as DataRow).Table.Rows.IndexOf((selectedCell.Item as DataRowView).Row);
            if (grd.CurrentColumn==null)
            {
                return;
            }
            int columnIndex = grd.CurrentColumn.DisplayIndex;//selectedCell.Column.DisplayIndex;

            DataGridRow row = (DataGridRow)grd.ItemContainerGenerator.ContainerFromIndex(grd.SelectedIndex);
            if (row == null)
                return;
            SingleData dataItem = (SingleData)row.DataContext;
            if (dataItem == null)
                return;
            if (columnIndex == 0)
            {
                SimpleIoc.Default.Unregister<PlanContentViewModel>();
                SimpleIoc.Default.Unregister<SingleData>();
                GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register(() => list[rowIndex]);
                GalaSoft.MvvmLight.Ioc.SimpleIoc.Default.Register<PlanContentViewModel>();
                PlanContentView planContentView = new PlanContentView();
                if (planContentView.ShowDialog() == true)
                {
                    var datas = AddOrUpdateTransPlanViewModel.GWCmdMakeUp.DataValue.Split(';');
                    datas[1 + rowIndex] = ServiceLocator.Current.GetInstance<PlanContentViewModel>().Plan.Name;
                    StringBuilder sb = new StringBuilder();
                    foreach (string str in datas)
                    {
                        sb.Append(str).Append(';');
                    }
                    string result = sb.ToString().TrimEnd(';');

                    dataItem.Name = datas[1 + rowIndex]; // 假设你想将Value属性设置为新
                    AddOrUpdateTransPlanViewModel.GWCmdMakeUp.DataValue = result;
                    //ServiceLocator.Current.GetInstance<PlanContentViewModel>().Plan;
                    /*gWCmdMakeUpService.Update(ModeBitViewModel.GWCmdMakeUp);
                    SetDataList[index].DataValue = ModeBitViewModel.GWCmdMakeUp.DataValue;*/
                }
            }
        }
    }
}
